<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="药品名称" prop="drugstoreName">
          <el-input
            v-model="queryParams.drugstoreName"
            placeholder="请输入药品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="drugstoreList">
        <el-table type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="rugstoreId" />
         <el-table-column label="库房" align="center" prop="warehouseName" />
        <el-table-column label="名称" align="center" prop="drugstoreName" />
        <el-table-column label="仓库数量" align="center" prop="drugstorenum" />
        <el-table-column label="供货商" align="center" prop="supplierName" />
        <el-table-column label="经办人" align="center" prop="skullName" />
        <el-table-column label="单位" align="center" prop="unitName" />
        <el-table-column label="批发价" align="center" prop="tradePrice" />
        <el-table-column label="售价" align="center" prop="sellingPrice" />
        <el-table-column label="产地" align="center" prop="areaName" />
        <el-table-column label="生产日期" align="center" prop="produceDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.produceDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="有效日期" align="center" prop="validDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.validDate, '{y}-{m}-{d}') }}</span>
          </template>
          </el-table-column>
          <el-table-column label="批号" align="center" prop="batch" />
          <el-table-column label="类型" align="center" prop="typeName" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:drugstore:edit']"
          >添加</el-button>
        </template>
      </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    <!-- 出库-->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数量" prop="drugstorenum">
          <el-input v-model="form.drugstorenum" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </div>
  </template>
  
  <script>
  import { listDrugstore,delDrugstore,updateDrugstore,getDrugstore} from "@/api/warehouse/drugstore";
  
  export default {
    name: "Drugstore",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 药品库存表格数据
        drugstoreList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          drugstoreName: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          drugstorenum: [
            { required: true, message: "数量不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询药品库存列表 */
      getList() {
        this.loading = true;
        listDrugstore(this.queryParams).then(response => {
          this.drugstoreList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          drugstorenum: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rugstoreId = row.rugstoreId || this.ids
      getDrugstore(rugstoreId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "添加数量";
       
      });
    },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.rugstoreId != null) {
              updateDrugstore(this.form).then(response => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const rugstoreIds = row.rugstoreId || this.ids;
        this.$modal.confirm('是否确认删除药品库存编号为"' + rugstoreIds + '"的数据项？').then(function() {
          return delDrugstore(rugstoreIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
  </script>
  